//
//  YKFarmFoodListViewController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/12.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmFoodListViewController.h"
#import "YKFarmViewModel.h"
#import <YK_TableRefresh/YK_TableRefresh.h>
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import "YKFarmFoodAddViewController.h"
#import "FarmFoodListCell.h"
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>

@interface YKFarmFoodListViewController ()<UITableViewDelegate,UITableViewDataSource>

///
@property(nonatomic,strong) YKFarmViewModel *viewModel;
///
@property(nonatomic,copy) NSString *farmId;
///
@property(nonatomic,strong) UITableView *tableView;
@end

@implementation YKFarmFoodListViewController

- (instancetype)initWithViewModel:(YKFarmViewModel *)viewModel farmId:(NSString *)farmId
{
    self = [super init];
    if (self) {
        self.farmId = farmId;
        self.viewModel = viewModel;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [btn setTitle:@"添加记录" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn sizeToFit];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    
    @weakify(self);
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"餐次" message:@"选择餐次" preferredStyle:UIAlertControllerStyleActionSheet];
        @weakify(self);
        UIAlertAction *morning = [UIAlertAction actionWithTitle:@"早餐" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self);
            NSString *MealTime = @"00";
            [self pushAdd:MealTime];
        }];
        UIAlertAction *lunch = [UIAlertAction actionWithTitle:@"午餐" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self);
            NSString *MealTime = @"01";
            [self pushAdd:MealTime];
            
        }];
        UIAlertAction *dinner = [UIAlertAction actionWithTitle:@"晚餐" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self);
            NSString *MealTime = @"02";
            [self pushAdd:MealTime];
            
        }];
        UIAlertAction *add = [UIAlertAction actionWithTitle:@"加餐" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            @strongify(self);
            NSString *MealTime = [NSString stringWithFormat:@"%02lu",(unsigned long)self.viewModel.foodArray.count];
            [self pushAdd:MealTime];
//            NSString *MealTime = [NSString stringWithFormat:@"",self.tableView.];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:morning];
        [alert addAction:lunch];
        [alert addAction:dinner];
        [alert addAction:add];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }];
    [self.view addSubview:self.tableView];
    [self.viewModel.farmFoodCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    [[self.viewModel.farmFoodCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在加载"];
        }else
        {
            [YKAlertCenter dissLoading];
        }
    }];
    [self.viewModel.saveFoodComm.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"保存成功"];
        [self.viewModel.farmFoodCommand execute:self.farmId];
    }];
    [self.viewModel.deleteFoodComm.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"删除成功"];
        [self.viewModel.farmFoodCommand execute:self.farmId];
    }];
    [self.viewModel.farmFoodCommand execute:self.farmId];
    // Do any additional setup after loading the view.
}

- (void)pushAdd:(NSString *)MealTime
{
    @weakify(self);
    YKFarmFoodAddViewController *vc = [[YKFarmFoodAddViewController alloc] initWithMealTime:MealTime farmId:self.farmId comfirmBlock:^(NSDictionary * _Nonnull params) {
        @strongify(self);
        [self.viewModel.saveFoodComm execute:params];
    }];
//    [self.navigationController pushViewController:vc animated:YES];
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark -tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.foodArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FarmFoodListCell *cell = [tableView dequeueReusableCellWithIdentifier:FarmFoodListCell.defaultIdentifier];
    [cell configDataWithViewModel:self.viewModel indexPath:indexPath dataSource:@[]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    @weakify(self);
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        /// todo
        @strongify(self);
        [self.viewModel getFoodDetail:indexPath block:^(NSString * _Nonnull MealTime, NSString * _Nonnull foodname, NSString * _Nonnull much, NSString * _Nonnull eattime, NSString * _Nonnull createTime, NSString * _Nonnull drugname, NSString * _Nonnull drugmuch, NSString * _Nonnull foodId) {
            [self.viewModel.deleteFoodComm execute:foodId];
        }];
    }];
    return @[deleteRowAction];
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 85;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.backgroundColor = UIColor.clearColor;
        @weakify(self);
        tableView.mj_header = [YKRefreshHeader headerNormalWithViewModel:self.viewModel dataSource:self.viewModel.farmListArray refreshingBlock:^{
            @strongify(self);
            [self.viewModel.farmFoodCommand execute:self.farmId];
        }];
        tableView.mj_footer = [YKRefreshFooter footerWithViewModel:self.viewModel refreshingBlock:^{
            @strongify(self);
            [self.viewModel.farmFoodCommand execute:self.farmId];
            
        }];
        [tableView registerClass:FarmFoodListCell.class forCellReuseIdentifier:FarmFoodListCell.defaultIdentifier];
        _tableView = tableView;
    }
    return _tableView;
}

- (void)dealloc
{
    [_viewModel clearFoodList];
}


@end
